package dahuiji.common.dao;

import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class BaceDao extends BaseDaoAbstract{
	protected Session session;
	protected Transaction tx;

	/**
	 * 使用hql语句进行分页查询
	 * 
	 * @param hql
	 *            需要查询的hql语句
	 * @param offset
	 *            第一条记录索引
	 * @param pageSize
	 *            每页需要显示的记录数
	 * @return 当前页的所有记录
	 */
	public List findByPage(final String hql, final int offset,
			final int pageSize) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();

		// 执行Hibernate分页查询
		List result = session.createQuery(hql).setFirstResult(offset)
				.setMaxResults(pageSize).list();
		tx.commit();
		session.close();
		return result;

	}

	/**
	 * 使用hql语句进行分页查询
	 * 
	 * @param hql
	 *            需要查询的hql语句
	 * @param value
	 *            如果hql有一个参数需要传入，value就是传入hql语句的参数
	 * @param offset
	 *            第一条记录索引
	 * @param pageSize
	 *            每页需要显示的记录数
	 * @return 当前页的所有记录
	 */
	public List findByPage(final String hql, final Object value,
			final int offset, final int pageSize) {
		// 通过一个HibernateCallback对象来执行查询
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();

		List result = session.createQuery(hql)
				// 为hql语句传入参数
				.setParameter(0, value).setFirstResult(offset)
				.setMaxResults(pageSize).list();
		tx.commit();
		session.close();
		return result;

	}

	/**
	 * 使用hql语句进行分页查询
	 * 
	 * @param hql
	 *            需要查询的hql语句
	 * @param values
	 *            如果hql有多个个参数需要传入，values就是传入hql的参数数组
	 * @param offset
	 *            第一条记录索引
	 * @param pageSize
	 *            每页需要显示的记录数
	 * @return 当前页的所有记录
	 */
	public List findByPage(final String hql, final Object[] values,
			final int offset, final int pageSize) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();

		Query query = session.createQuery(hql);
		// 为hql语句传入参数
		for (int i = 0; i < values.length; i++) {
			query.setParameter(i, values[i]);
		}
		List result = query.setFirstResult(offset).setMaxResults(pageSize)
				.list();
		tx.commit();
		session.close();
		return result;

	}

	/**
	 * 保存
	 * 
	 * @param entity
	 * @return
	 */
	public Long save(Object entity) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();
		Long id = (Long) session.save(entity);
		tx.commit();
		session.close();

		return id;
	}

	/**
	 * 更新或者保存
	 * 
	 * @param entity
	 */
	public void saveOrUpdate(Object entity) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();

		session.saveOrUpdate(entity);
		tx.commit();
		session.close();

	}

	/**
	 * 更新
	 * 
	 * @param entity
	 * @return
	 */
	public void update(Object entity) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();

		session.update(entity);
		tx.commit();
		session.close();

	}

	/**
	 * 删除
	 * 
	 * @param entity
	 */
	public void delete(Object entity) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();
		session.delete(entity);
		tx.commit();
		session.close();

	}

	/**
	 * 得到实体
	 * 
	 * @param entity
	 * @param id
	 * @return
	 */
	public Object getEntity(Class entity, Long id) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();
		Object object = session.get(entity, id);
		tx.commit();
		session.close();

		return object;
	}

	/**
	 * 执行hql语句
	 * 
	 * @param hql
	 * @return
	 */
	public int executeHql(String hql) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();
		Query queryObject = session.createQuery(hql);
		int i = queryObject.executeUpdate();
		tx.commit();
		session.close();

		return i;

	}

	/**
	 * 执行sql查询语句
	 * 
	 * @param sql
	 * @return
	 */

	public List executeQuerySql(String sql) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();
		List list = session.createSQLQuery(sql).list();
		tx.commit();
		session.close();
		return list;

	}
	public List execuQueryHql(String hql){
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();
		List list = session.createQuery(hql).list();
		tx.commit();
		session.close();
		return list;
	}

	/**
	 * 执行sql查询
	 * 
	 * @param sql
	 * @return
	 */
	public void executeBySql(String sql) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();
		Query queryObject = session.createSQLQuery(sql);
		queryObject.executeUpdate();
		tx.commit();
		session.close();
		
	}

	/**
	 * 执行sql翻页查询
	 * 
	 * @param sql
	 * @param values
	 * @param offset
	 * @param pageSize
	 * @return
	 */
	public List findBySqlPage(final String sql, final Object[] values,
			final int offset, final int pageSize) {
		session = this.getSessionFactory().openSession();
		tx = session.beginTransaction();

		Query query = session.createSQLQuery(sql);
		// 为hql语句传入参数
		for (int i = 0; i < values.length; i++) {
			query.setParameter(i, values[i]);
		}
		List result = query.setFirstResult(offset).setMaxResults(pageSize)
				.list();
		tx.commit();
		session.close();

		return result;

	}
}
